package web.controller.employer;

import common.consts.ContentType;
import common.validation.ValidationResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import service.RejectService;
import service.ServiceResponse;
import web.Routes;
import web.controller.Base;
import web.req.RejectRequest;
import web.security.Protection;

/**
 * Created by aceyin on 15/7/2.
 */
@RestController
@RequestMapping(Routes.Employer.REJECT)
public class Reject extends Base {
    @Autowired
    private RejectService rejectService;

    @Protection
    @RequestMapping(method = RequestMethod.POST, produces = ContentType.JSON)
    public ServiceResponse reject(@RequestBody RejectRequest request) {
        ValidationResult result = request.validate();
        if (result.isFailed()) {
            return fail(result.getMessage());
        }

        long employer = getUserIdFromAuthHeader();
        Long applier = request.getApplier();
        long recruitmentId = request.getRecruitment();
        if (applier == null || applier <= 0) {
            return rejectService.rejectAll(employer, recruitmentId);
        } else {
            return rejectService.reject(employer, applier, recruitmentId);
        }
    }
}
